<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/18
 * Time: 16:42
 */
class Http
{
    //监听ip
    protected $host;
    //监听端口
    protected $port;
    //服务对象
    protected $httpServer;

    public function __construct($port = 9051, $host = '0.0.0.0')
    {
        $this->host = $host;
        $this->port = $port;
        // 实例化swoole http对象
        $this->httpServer = new Swoole\Http\Server($this->host, $this->port);
        //设置访问静态资源文件
        $this->httpServer->set([
            'document_root' => 'G:\haibei\websocket\swoole\demo\static',
            'enable_static_handler' => true,
        ]);
        //监听请求事件
        $this->httpServer->on('request', [$this, 'http_request']);
        // 开启服务
        $this->httpServer->start();
    }
    //监听http请求事件
    public static function http_request(Swoole_Http_Request $request, Swoole_Http_Response $response)
    {
        echo '请求成功' . $request->fd;
        //返回客户端消息
        $response->end("<head><meta charset='UTF-8'></head><h1>请求成功. #" . rand(1000, 9999) . "</h1>");
    }

}
$re = new Http(9513);


